{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2e6b89c7",
   "metadata": {},
   "source": [
    "|字典方法      |说明   | example  | 备注  \n",
    "| ----------- | ----------- | ----------- | ----------- |\n",
    "| clear()      | 删除字典内所有元素       |user_dict.clear() |\n",
    "|copy()      |复制字典内所有元素         |user_dict.copy()  |\n",
    "|fromkeys()   |创建新字典    |dict.fromkeys()  |\n",
    "|get()     |返回指定键         |user_dict.get()  |\n",
    "|items()    |返回一个dict_items对象         |user_dict.items()  |\n",
    "|keys()     |返回一个dict_keys对象         |user_dict.keys()  |\n",
    "|values()    |返回一个dict_values对象  |user_dict.values()  |\n",
    "|pop()      |删除键值对，返回相应的值          |user_dict.pop()   |\n",
    "popitem()     |随机返回并删除字典中最后一个键值对        |user_dict.popitem()  |\n",
    "setdefault()    |返回指定键的值          |user_dict.setdefault()   |\n",
    "update()      |更新已有的字典           |user_dict.update({})   |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a14ac8b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on class dict in module builtins:\n",
      "\n",
      "class dict(object)\n",
      " |  dict() -> new empty dictionary\n",
      " |  dict(mapping) -> new dictionary initialized from a mapping object's\n",
      " |      (key, value) pairs\n",
      " |  dict(iterable) -> new dictionary initialized as if via:\n",
      " |      d = {}\n",
      " |      for k, v in iterable:\n",
      " |          d[k] = v\n",
      " |  dict(**kwargs) -> new dictionary initialized with the name=value pairs\n",
      " |      in the keyword argument list.  For example:  dict(one=1, two=2)\n",
      " |  \n",
      " |  Built-in subclasses:\n",
      " |      StgDict\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      True if the dictionary has the specified key, else False.\n",
      " |  \n",
      " |  __delitem__(self, key, /)\n",
      " |      Delete self[key].\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(...)\n",
      " |      x.__getitem__(y) <==> x[y]\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __init__(self, /, *args, **kwargs)\n",
      " |      Initialize self.  See help(type(self)) for accurate signature.\n",
      " |  \n",
      " |  __ior__(self, value, /)\n",
      " |      Return self|=value.\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __or__(self, value, /)\n",
      " |      Return self|value.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __reversed__(self, /)\n",
      " |      Return a reverse iterator over the dict keys.\n",
      " |  \n",
      " |  __ror__(self, value, /)\n",
      " |      Return value|self.\n",
      " |  \n",
      " |  __setitem__(self, key, value, /)\n",
      " |      Set self[key] to value.\n",
      " |  \n",
      " |  __sizeof__(...)\n",
      " |      D.__sizeof__() -> size of D in memory, in bytes\n",
      " |  \n",
      " |  clear(...)\n",
      " |      D.clear() -> None.  Remove all items from D.\n",
      " |  \n",
      " |  copy(...)\n",
      " |      D.copy() -> a shallow copy of D\n",
      " |  \n",
      " |  get(self, key, default=None, /)\n",
      " |      Return the value for key if key is in the dictionary, else default.\n",
      " |  \n",
      " |  items(...)\n",
      " |      D.items() -> a set-like object providing a view on D's items\n",
      " |  \n",
      " |  keys(...)\n",
      " |      D.keys() -> a set-like object providing a view on D's keys\n",
      " |  \n",
      " |  pop(...)\n",
      " |      D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      " |      \n",
      " |      If key is not found, default is returned if given, otherwise KeyError is raised\n",
      " |  \n",
      " |  popitem(self, /)\n",
      " |      Remove and return a (key, value) pair as a 2-tuple.\n",
      " |      \n",
      " |      Pairs are returned in LIFO (last-in, first-out) order.\n",
      " |      Raises KeyError if the dict is empty.\n",
      " |  \n",
      " |  setdefault(self, key, default=None, /)\n",
      " |      Insert key with a value of default if key is not in the dictionary.\n",
      " |      \n",
      " |      Return the value for key if key is in the dictionary, else default.\n",
      " |  \n",
      " |  update(...)\n",
      " |      D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.\n",
      " |      If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]\n",
      " |      If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v\n",
      " |      In either case, this is followed by: for k in F:  D[k] = F[k]\n",
      " |  \n",
      " |  values(...)\n",
      " |      D.values() -> an object providing a view on D's values\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods defined here:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      " |  \n",
      " |  fromkeys(iterable, value=None, /) from builtins.type\n",
      " |      Create a new dictionary with keys from iterable and values set to value.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  __hash__ = None\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bbf8509d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '11', 'age': '20', 'job': '工程师'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict = {'name':\"11\",\n",
    "             'age':\"20\",\n",
    "             'job':\"工程师\"}\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1466a4a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict.clear()\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "861f700d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '11', 'age': '20', 'job': '工程师'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict = {'name':\"11\",\n",
    "             'age':\"20\",\n",
    "             'job':\"工程师\"}\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7038f483",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '11', 'age': '20', 'job': '工程师'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict.copy()\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "728fcb0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2': None, '1': None}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict.fromkeys(\"21\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7d4f068f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2: 'wow', 1: 'wow'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict.fromkeys((2,1),\"wow\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "44d48693",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '11', 'age': '20', 'job': '工程师'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict = {'name':\"11\",\n",
    "             'age':\"20\",\n",
    "             'job':\"工程师\"}\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f0a68d01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'11'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict.get(\"name\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "78a445bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '11', 'age': '20', 'job': '工程师'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict = {'name':\"11\",\n",
    "             'age':\"20\",\n",
    "             'job':\"工程师\"}\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "50df75a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('name', '11'), ('age', '20'), ('job', '工程师')])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d476d91b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '11', 'age': '20', 'job': '工程师'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict = {'name':\"11\",\n",
    "             'age':\"20\",\n",
    "             'job':\"工程师\"}\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "48cf97fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['name', 'age', 'job'])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c046cf4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '11', 'age': '20', 'job': '工程师'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict = {'name':\"11\",\n",
    "             'age':\"20\",\n",
    "             'job':\"工程师\"}\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "29791ef6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['11', '20', '工程师'])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e923132c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '11', 'age': '20', 'job': '工程师'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict = {'name':\"11\",\n",
    "             'age':\"20\",\n",
    "             'job':\"工程师\"}\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e7f14631",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'age': '20', 'job': '工程师'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict.pop(\"name\")\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "4bee9474",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('job', '工程师')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict = {'name':\"11\",\n",
    "             'age':\"20\",\n",
    "             'job':\"工程师\"}\n",
    "user_dict\n",
    "user_dict.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "99188188",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'11'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict = {'name':\"11\",\n",
    "             'age':\"20\",\n",
    "             'job':\"工程师\"}\n",
    "user_dict\n",
    "user_dict.setdefault(\"name\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ff109254",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'name': '11', 'age': '20', 'job': '工程师', 'money': None}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict.setdefault(\"money\")\n",
    "print(user_dict.setdefault(\"money\"))\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "969971a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '11', 'age': '20', 'job': '工程师', 'money': '8k'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict = {'name':\"11\",\n",
    "             'age':\"20\",\n",
    "             'job':\"工程师\"}\n",
    "user_dict\n",
    "user_dict.update({\"money\":\"8k\"})\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6978f6eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '22', 'age': '20', 'job': '工程师'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict = {'name':\"11\",\n",
    "             'age':\"20\",\n",
    "             'job':\"工程师\"}\n",
    "user_dict\n",
    "user_dict.update({\"name\":\"22\"})\n",
    "user_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "953d84fa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
